﻿CREATE PROCEDURE [dbo].[InputLinesRefresh]
(
	@DocId int,
	@GoodsId int,
	@SupplierGoodsId int,
	@ManufacturerPrice decimal(19, 2),
	@SupplierPrice decimal(19, 2),
	@SupplierSumNOTAX decimal(19, 2),
	@ImportMarkup float,
	@SupplierMarkup float,
	@BatchNumber varchar(20),
	@ReadyBefore smalldatetime,
	@MadeDate smalldatetime,
	@NDSTax tinyint,
	@NDSValue decimal(19, 2),
	@ReestrPrice decimal(19, 2),
	@ReestrDate smalldatetime,
	@GTDNumber varchar(30),
	@CertificateNumber varchar(80),
	@CertificateDate smalldatetime,
	@CertificateOrgId int,
	@RetailMarkup float,
	@ContragentId int,
	@ContragentDocDate smalldatetime,
	@ContragentDocNumber varchar(20),
	@Note varchar(250),
	@MD datetime,
	@MW varchar(50), 
	@Original_Id int     --CD.InputLines.Id
)
AS
DECLARE @bitDoRefresh bit
DECLARE @orgGoodsId int,
@orgSupplierGoodsId int,
@orgManufacturerPrice decimal(19, 2),
@orgSupplierPrice decimal(19, 2),
@orgSupplierSumNOTAX decimal(19, 2),
@orgImportMarkup float,
@orgSupplierMarkup float,
@orgBatchNumber varchar(20),
@orgReadyBefore smalldatetime,
@orgMadeDate smalldatetime,
@orgNDSTax tinyint,
@orgNDSValue decimal(19, 2),
@orgReestrPrice decimal(19, 2),
@orgReestrDate smalldatetime,
@orgGTDNumber varchar(30),
@orgCertificateNumber varchar(80),
@orgCertificateDate smalldatetime,
@orgCertificateOrgId int,
@orgRetailMarkup float,
@orgContragentId int,
@orgContragentDocDate smalldatetime,
@orgContragentDocNumber varchar(20)

SET NOCOUNT ON

SET @bitDoRefresh = 0

SELECT @orgGoodsId = GoodsId,
@orgSupplierGoodsId = SupplierGoodsId,
@orgManufacturerPrice = ManufacturerPrice,
@orgSupplierPrice = SupplierPrice,
@orgSupplierSumNOTAX = SupplierSumNOTAX,
@orgImportMarkup = ImportMarkup,
@orgSupplierMarkup = SupplierMarkup,
@orgBatchNumber = BatchNumber,
@orgReadyBefore = ReadyBefore,
@orgMadeDate = MadeDate,
@orgNDSTax = NDSTax,
@orgNDSValue = NDSValue,
@orgReestrPrice = ReestrPrice,
@orgReestrDate = ReestrDate,
@orgGTDNumber = GTDNumber,
@orgCertificateNumber = CertificateNumber,
@orgCertificateDate = CertificateDate,
@orgCertificateOrgId = CertificateOrgId,
@orgRetailMarkup = RetailMarkup,
@orgContragentId = ContragentId,
@orgContragentDocDate = ContragentDocDate,
@orgContragentDocNumber = ContragentDocNumber
FROM CD.InputLines WHERE Id = @Original_Id

IF @orgGoodsId <> @GoodsId SET @bitDoRefresh = 1
IF @orgSupplierGoodsId = @SupplierGoodsId SET @bitDoRefresh = 1
IF @orgManufacturerPrice <> @ManufacturerPrice SET @bitDoRefresh = 1
IF @orgSupplierPrice <> @SupplierPrice SET @bitDoRefresh = 1
IF @orgSupplierSumNOTAX <> @SupplierSumNOTAX SET @bitDoRefresh = 1
IF @orgImportMarkup <> @ImportMarkup SET @bitDoRefresh = 1
IF @orgSupplierMarkup <> @SupplierMarkup SET @bitDoRefresh = 1
IF @orgBatchNumber <> @BatchNumber SET @bitDoRefresh = 1
IF @orgReadyBefore <> @ReadyBefore SET @bitDoRefresh = 1
IF @orgMadeDate <> @MadeDate SET @bitDoRefresh = 1
IF @orgNDSTax <> @NDSTax SET @bitDoRefresh = 1
IF @orgNDSValue <> @NDSValue SET @bitDoRefresh = 1
IF @orgReestrPrice <> @ReestrPrice SET @bitDoRefresh = 1
IF @orgReestrDate <> @ReestrDate SET @bitDoRefresh = 1
IF @orgGTDNumber <> @GTDNumber SET @bitDoRefresh = 1
IF @orgCertificateNumber <> @CertificateNumber SET @bitDoRefresh = 1
IF @orgCertificateDate <> @CertificateDate SET @bitDoRefresh = 1
IF @orgCertificateOrgId <> @CertificateOrgId SET @bitDoRefresh = 1
IF @orgRetailMarkup <> @RetailMarkup SET @bitDoRefresh = 1

IF @orgContragentId <> @ContragentId SET @bitDoRefresh = 1
IF @orgContragentDocDate <> @ContragentDocDate SET @bitDoRefresh = 1
IF @orgContragentDocNumber <> @ContragentDocNumber SET @bitDoRefresh = 1

IF @bitDoRefresh = 1
	UPDATE CD.InputLines  SET
	GoodsId = @GoodsId,
	SupplierGoodsId = @SupplierGoodsId,
	ManufacturerPrice = @ManufacturerPrice,
	SupplierPrice = @SupplierPrice,
	SupplierSumNOTAX = @SupplierSumNOTAX,
	ImportMarkup = @ImportMarkup,
	SupplierMarkup = @SupplierMarkup,
	BatchNumber = @BatchNumber,
	ReadyBefore = @ReadyBefore,
	MadeDate = @MadeDate,
	NDSTax = @NDSTax,
	NDSValue = @NDSValue,
	ReestrPrice = @ReestrPrice,
	ReestrDate = @ReestrDate,
	GTDNumber = @GTDNumber,
	CertificateNumber = @CertificateNumber,
	CertificateDate = @CertificateDate,
	CertificateOrgId = @CertificateOrgId,
	RetailMarkup = @RetailMarkup,
	Note = @Note,
	MD = @MD,
	MW	= @MW
	WHERE Id = @Original_Id

RETURN 0


